package com.view;

import com.util.DimensionUtil;
import com.view.component.AnnouncementComponent;

import javax.swing.*;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

public class MainView {

    public MainView() {
        JFrame jFrame = new JFrame("CC小区物业管理系统");
//        Container contentPane = getContentPane();

        //设置分割面板
        JSplitPane sp = new JSplitPane();

        //支持连续布局
        sp.setContinuousLayout(true);
        sp.setDividerLocation(200); //分割条初始从左往右200px
        sp.setDividerSize(7);   //分割条宽度7px

        //设置左侧内容（管理员）
        DefaultMutableTreeNode tree_1 = new DefaultMutableTreeNode("管理员系统管理");
        DefaultMutableTreeNode tree_owner = new DefaultMutableTreeNode("业主信息管理");
        DefaultMutableTreeNode tree_house = new DefaultMutableTreeNode("房产信息管理");
        DefaultMutableTreeNode tree_work = new DefaultMutableTreeNode("保安保洁指派");
        DefaultMutableTreeNode tree_payment = new DefaultMutableTreeNode("缴费管理");
        DefaultMutableTreeNode tree_announcement = new DefaultMutableTreeNode("小区公告管理");
        DefaultMutableTreeNode tree_service = new DefaultMutableTreeNode("维修维护管理");

        //添加至面板
        tree_1.add(tree_owner);
        tree_1.add(tree_house);
        tree_1.add(tree_work);
        tree_1.add(tree_payment);
        tree_1.add(tree_announcement);
        tree_1.add(tree_service);

        JTree tree_admin = new JTree(tree_1);

        //设置左侧栏背景颜色
//        tree_admin.setBackground(Color.cyan);

        //输入几默认选中几
        tree_admin.setSelectionRow(0);
        tree_admin.addTreeSelectionListener(new TreeSelectionListener() {
            //当条目选中变化后，这个方法会执行
            @Override
            public void valueChanged(TreeSelectionEvent e) {
                //得到当前选中的节点对象
                Object lastPathComponent = e.getNewLeadSelectionPath().getLastPathComponent();

                if (tree_announcement.equals(lastPathComponent)) {
                    sp.setRightComponent(new AnnouncementComponent(jFrame));
                    sp.setDividerLocation(150);
                } else if (tree_service.equals(lastPathComponent)) {
                    sp.setRightComponent(new JLabel("维修维护功能正在开发，敬请期待...", SwingConstants.CENTER));
                    sp.setDividerLocation(150);
                } else if (tree_payment.equals(lastPathComponent)) {
                    sp.setRightComponent(new JLabel("缴费功能正在开发，敬请期待...", SwingConstants.CENTER));
                    sp.setDividerLocation(150);
                } else if (tree_work.equals(lastPathComponent)) {
                    sp.setRightComponent(new JLabel("保安保洁指派正在开发，敬请期待...", SwingConstants.CENTER));
                    sp.setDividerLocation(150);
                } else if (tree_house.equals(lastPathComponent)) {
                    sp.setRightComponent(new JLabel("房产信息管理正在开发，敬请期待...", SwingConstants.CENTER));
                    sp.setDividerLocation(150);
                } else if (tree_owner.equals(lastPathComponent)) {
                    sp.setRightComponent(new JLabel("业主管理功能正在开发，敬请期待...", SwingConstants.CENTER));
                    sp.setDividerLocation(150);
                }
            }
        });

        sp.setRightComponent(new JLabel("欢迎进入管理员操作系统,请通过左侧菜单栏选择你需要实用的功能", SwingConstants.CENTER));
        sp.setLeftComponent(tree_admin);
        jFrame.add(sp);

        //根据屏幕大小设置主界面大小
        jFrame.setBounds(DimensionUtil.getBounds());

        //设置窗体完全充满整个屏幕的可见大小
        jFrame.setExtendedState(jFrame.MAXIMIZED_BOTH);
        jFrame.setResizable(true);

        //设置窗体图标
//        URL resource = FirstScene.class.getClassLoader().getResource("Icon.jpg");
//        Image image = new ImageIcon(resource).getImage();
//        setIconImage(image);
        Image icon = Toolkit.getDefaultToolkit().getImage("src/resource/Icon.jpg");
        jFrame.setIconImage(icon);

        jFrame.setLocationRelativeTo(null);
        jFrame.setDefaultCloseOperation(jFrame.EXIT_ON_CLOSE);
        jFrame.setVisible(true);

        //菜单栏
        JMenuBar jmBar = new JMenuBar();

        JMenu jmenu = new JMenu("菜单");
        JMenu jmenu2 = new JMenu("编辑");

        //jmenu
        JMenu jm1sub = new JMenu("软件项目");
        JMenuItem jm1item1 = new JMenuItem("切换账号");
        JMenuItem jm1item2 = new JMenuItem("退出程序");
        jm1item1.setAccelerator(KeyStroke.getKeyStroke('A'));
        jm1item2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK));
        jm1item1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    new LoginView();
                    jFrame.dispose();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });
        jm1item2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        JMenuItem subDown1 = new JMenuItem("物业管理系统");
        JMenuItem subDown2 = new JMenuItem("业主服务系统");

        //jmenu2
        JMenuItem jm2item1 = new JMenuItem("暂无选项");
        JMenuItem jm2item2 = new JMenuItem("测试");

        jmenu.add(jm1item1);
        jmenu.add(jm1item2);
        //菜单下拉列表后相隔的横线
        jmenu.addSeparator();
        jmenu.add(jm1sub);
        jm1sub.add(subDown1);
        jm1sub.add(subDown2);

        jmenu2.add(jm2item1);
        jmenu2.add(jm2item1);

        jmBar.add(jmenu);
        jmBar.add(jmenu2);

        jFrame.setJMenuBar(jmBar);

        //字体设置
        Font jmenuFont = new Font("楷体", Font.PLAIN, 18);
        Font downList = new Font("楷体", Font.PLAIN, 16);
        jmenu.setFont(jmenuFont);
        jmenu2.setFont(jmenuFont);
        jm1sub.setFont(downList);
        jm1item1.setFont(downList);
        jm1item2.setFont(downList);
        subDown1.setFont(downList);
        subDown2.setFont(downList);
        jm2item1.setFont(downList);
        jm2item2.setFont(downList);


    }

    //菜单树状栏图标绘制
//    private class MyRenderer extends DefaultTreeCellRenderer{
//        private Image tree_1Icon = null;
//        private Image tree_ownerIcon = null;
//        private Image tree_houseIcon = null;
//        private Image tree_workIcon = null;
//        private Image tree_paymentIcon = null;
//        private Image tree_announcementIcon = null;
//        private Image tree_serviceIcon = null;
//
//        public MyRenderer(){
//            tree_1Icon = ImageIO.read(new File(PathUtils.getRealPath("")));
//
//        }
//
//        @Override
//        public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
//            return super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
//
//        }
//    }

    public static void main(String[] args) {
        new MainView();
    }
}
